//
//  TChatController.m
//  UIKit
//
//  Created by kennethmiao on 2018/9/18.
//  Copyright © 2018年 kennethmiao. All rights reserved.
//

#import "TChatController.h"
#import "THeader.h"
#import "ChatMessageEditViewController.h"
#import "ChatMessageManageViewController.h"

@interface TChatController () <TMessageControllerDelegate, TInputControllerDelegate, ChatTopToolBarViewDelegate>
@property (nonatomic, strong) PSChatSentenceView *chatSentenceView;

@property (nonatomic , assign)CGFloat chatViewHeight;

@property (nonatomic , assign)CGFloat inputViewY;


@end

@implementation TChatController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupViews];
}
- (void)setupViews {
    self.view.backgroundColor = THEMECOLOR_OF_WHITE;
    self.view.v_h -= (kTopHeight-kStatusBarHeight);
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 44, 44);
    //    leftButton.imageEdgeInsets = UIEdgeInsetsMake(11, 0, 12, 32);
    [leftButton setImage:[UIImage imageNamed:@"back_b"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(leftBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -10.0f;
    if (([[[UIDevice currentDevice] systemVersion] floatValue] >= 11.0)) {
        leftButton.contentEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
        leftButton.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 0);
    }
    self.navigationItem.leftBarButtonItems = @[spaceItem, leftItem];
//    self.parentViewController.navigationItem.leftBarButtonItems = @[spaceItem, leftItem];
    
    if (self.isNotice) { // 官方通知
        //message
        _messageController = [[TMessageController alloc] init];
        _messageController.isSessionPush = self.isSessionPush;
        _messageController.view.frame = CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height);
        _messageController.chatAvatar = ((self.infoModel.avatar&&self.infoModel.avatar.length)?self.infoModel.avatar:@"");
        _messageController.delegate = self;
        _messageController.isNotice = self.isNotice;
        [self addChildViewController:_messageController];
        [self.view addSubview:_messageController.view];
        [_messageController setConversation:_conversation];
    } else {
        UIBarButtonItem *yuemianshi = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"duihua_yaoyuexin"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(yuemianshiTap)];
        UIBarButtonItem *shezhi = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"duihua_shezhi"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(shezhiTap)];
        if ([UD_GetOForK(isUserRole) isEqualToString:@"1"]) {
            self.navigationItem.rightBarButtonItems = @[shezhi, yuemianshi]; self.parentViewController.navigationItem.rightBarButtonItems = @[shezhi, yuemianshi];
        } else {
            self.navigationItem.rightBarButtonItem = shezhi; self.parentViewController.navigationItem.rightBarButtonItem = shezhi;
        }
        
        self.CTTBView = [[ChatTopToolBarView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80*Scale) target:self];
        self.CTTBView.backgroundColor = COLOR_OF_WHITE;
        [self.view addSubview:self.CTTBView];
        [self.CTTBView borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:lineH borderType:BorderSideTypeBottom];
        if (self.infoModel.nav.namecard.disabled&&self.infoModel.nav.namecard.disabled.length) {
            [self.CTTBView jiaohuanmingpianCanUse:[self.infoModel.nav.namecard.disabled isEqualToString:@"1"]?NO:YES];
        }
        if (self.infoModel.nav.send_resume.disabled&&self.infoModel.nav.send_resume.disabled.length) {
            [self.CTTBView fasongjianliCanUse:[self.infoModel.nav.send_resume.disabled isEqualToString:@"1"]?NO:YES];
        }
        if (self.infoModel.nav.apply_resume.disabled&&self.infoModel.nav.apply_resume.disabled.length) {
            [self.CTTBView shenqingjianliCanUse:[self.infoModel.nav.apply_resume.disabled isEqualToString:@"1"]?NO:YES];
        }
        if (self.infoModel.nav.video_interview.disabled&&self.infoModel.nav.video_interview.disabled.length) {
            [self.CTTBView shipinmianshiCanUse:[self.infoModel.nav.video_interview.disabled isEqualToString:@"1"]?NO:YES];
        }
        
        //message
        _messageController = [[TMessageController alloc] init];
        _messageController.view.frame = CGRectMake(0, EndY(self.CTTBView), WIDTH, self.view.frame.size.height-TTextView_Height -SIMEmojKeyBoardkBottomNOSafeArea_Height-ViewHeight(self.CTTBView));
        _messageController.chatAvatar = ((self.infoModel.avatar&&self.infoModel.avatar.length)?self.infoModel.avatar:@"");
        _messageController.isSessionPush = self.isSessionPush;
        _messageController.delegate = self;
        _messageController.isNotice = self.isNotice;
        [self addChildViewController:_messageController];
        [self.view addSubview:_messageController.view];
        //        [_messageController setConversation:_conversation];
        if (!self.isSessionPush) {
            TIMConversation *conv = [[TIMManager sharedInstance] getConversation:(TIMConversationType)_conversation.convType receiver:_conversation.convId];
            __weak __typeof(self)weakSelf = self;
            [conv setReadMessage:nil succ:^{
                NSLog(@"");
                __strong __typeof(weakSelf)strongSelf = weakSelf;
                [strongSelf.messageController setConversation:strongSelf.conversation];
            } fail:^(int code, NSString *msg) {
                NSLog(@"");
                __strong __typeof(weakSelf)strongSelf = weakSelf;
                [strongSelf.messageController setConversation:strongSelf.conversation];
            }];
        } else {
            [_messageController setConversation:_conversation];
        }
        
        //input
        _inputController = [[TInputController alloc] init];
        //        _inputController.view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - TTextView_Height - Bottom_SafeHeight-HeightOfStatusBarAndNavigationBar, self.view.frame.size.width, TTextView_Height + Bottom_SafeHeight);
        _inputController.view.frame = CGRectMake(0, self.view.frame.size.height - TTextView_Height - SIMEmojKeyBoardkBottomNOSafeArea_Height, self.view.frame.size.width, TTextView_Height + SIMEmojKeyBoardkBottomNOSafeArea_Height);
        _inputController.delegate = self;
        [self addChildViewController:_inputController];
        [self.view addSubview:_inputController.view];
        _chatViewHeight = _messageController.view.v_h;
        _inputViewY = _inputController.view.v_y;
    }
}

- (void)yuemianshiTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatControllerDidClickRightBarBtnYuemianshi:)]) {
        [self.delegate chatControllerDidClickRightBarBtnYuemianshi:self];
    }
}
- (void)shezhiTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatControllerDidClickRightBarBtnShezhi:)]) {
        [self.delegate chatControllerDidClickRightBarBtnShezhi:self];
    }
}
- (void)rightBarButtonClick{
    if(_delegate && [_delegate respondsToSelector:@selector(chatControllerDidClickRightBarButton:)]){
        [_delegate chatControllerDidClickRightBarButton:self];
    }
}
- (void)leftBarButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)inputController:(TInputController *)inputController didChangeHeight:(CGFloat)height keyBoardShow:(BOOL)isShow{
//    if (SIMEmojKeyBoardisFullProfiledScreen) {
//        height-=kStatusBarHeight;
//    }
    __weak typeof(self) ws = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        CGRect msgFrame = ws.messageController.view.frame;
        msgFrame.size.height = ws.view.frame.size.height - height;
        ws.messageController.view.frame = msgFrame;

        CGRect inputFrame = ws.inputController.view.frame;
        inputFrame.origin.y = msgFrame.origin.y + msgFrame.size.height;
        inputFrame.size.height = height;
        ws.inputController.view.frame = inputFrame;

        [ws.messageController scrollToBottom:NO];
    } completion:nil];

}
- (void)inputController:(TInputController *)inputController didSendMessage:(TMessageCellData *)msg {
    [_messageController sendMessage:msg];
}
- (void)inputController:(TInputController *)inputController didSelectMoreAtIndex:(NSInteger)index {
    if(_delegate && [_delegate respondsToSelector:@selector(chatController:didSelectMoreAtIndex:)]){
        [_delegate chatController:self didSelectMoreAtIndex:index];
    }
}
- (void)inputControllerDidChatSentenceViewAdd:(TInputController *)inputController {
    NSLog(@"inputControllerDidChatSentenceViewAdd");
    ChatMessageEditViewController *CMEVC = [[ChatMessageEditViewController alloc] init];
    [self.navigationController pushViewController:CMEVC animated:YES];
}
- (void)inputControllerDidChatSentenceViewManage:(TInputController *)inputController {
    NSLog(@"inputControllerDidChatSentenceViewManage");
    ChatMessageManageViewController *CMMVC = [[ChatMessageManageViewController alloc] init];
    [self.navigationController pushViewController:CMMVC animated:YES];
}
- (void)didTapInMessageController:(TMessageController *)controller {
    [_inputController reset];
}
- (BOOL)messageController:(TMessageController *)controller willShowMenuInCell:(TMessageCell *)cell {
    if([_inputController.textView.inputTextView isFirstResponder]) { _inputController.textView.inputTextView.overrideNextResponder = cell;
        return YES;
    }
    return NO;
}
- (void)didHideMenuInMessageController:(TMessageController *)controller { _inputController.textView.inputTextView.overrideNextResponder = nil;
}
- (void)messageController:(TMessageController *)controller didSelectMessages:(NSMutableArray *)msgs atIndex:(NSInteger)index {
    if(_delegate && [_delegate respondsToSelector:@selector(chatController:didSelectMessages:atIndex:)]){
        [_delegate chatController:self didSelectMessages:msgs atIndex:index];
    }
}
- (void)sendImageMessage:(UIImage *)image; {
    [_messageController sendImageMessage:image];
}
- (void)sendVideoMessage:(NSURL *)url {
    [_messageController sendVideoMessage:url];
}
- (void)sendFileMessage:(NSURL *)url {
    [_messageController sendFileMessage:url];
}
- (void)sendWeizhi:(NSDictionary *)dic {
    [_messageController sendWeizhi:dic];
}
- (void)chatTopToolBarViewTap:(ChatTopToolBarViewType)chatTopToolBarViewType {
    if (self.delegate && [self.delegate respondsToSelector:@selector(chatController:didSelectChatItem:)]) {
        [self.delegate chatController:self didSelectChatItem:chatTopToolBarViewType];
    }
}

@end
